﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HF.WorkFlow.DataContract;
using HF.WCF.WFClientAgent;
using HF.WorkFlow.Const;
using HF.Framework;
using HF.WCF.WFClientAgent.WorkFlowServiceReference;
namespace HFWcfClientDemo
{
    public partial class Form1 : Form
    {
        ArchitectureClient client = null;
        WorkFlowClient wfClient = null;
        UserClient userClient = null;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //获取所有部门
            //List<ArchitectureEntity> entitys = client.GetAllDepartmentEntitys();

            WebＡrchitectureServices.ArchitectureServiceClient architecureClient = new WebＡrchitectureServices.ArchitectureServiceClient();
            ArchitectureEntity[] entitys = architecureClient.GetAllDepartmentEntitys();

            dataGridView1.DataSource = entitys;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dtpStartTime.Value = DateTime.Now; 
            dtpEndTime.Value=dtpStartTime.Value.AddDays(10);
            client = ArchitectureClient.GetInstance();
            wfClient = WorkFlowClient.GetInstance();
            userClient = UserClient.GetInstance();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //读取组织机构下的所有人
            string archid = "0454b1b5-17bf-44ed-9066-7dc5056f710c";
            List<UserEntity> entitys = client.GetUserEntitys(archid);
            dataGridView1.DataSource = entitys;
           
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //用户所隶属的所有部门
            string userId="50009";
            List<ArchitectureEntity> entitys = client.GetDepartmentEntitysOfUser(userId);
            dataGridView1.DataSource = entitys;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //获取子机构
            string archid = "0454b1b5-17bf-44ed-9066-7dc5056f710c";
            List<ArchitectureEntity> entitys = client.GetChildArchitectureEntitys(archid);
            dataGridView1.DataSource = entitys;

        }

        private void button5_Click(object sender, EventArgs e)
        {
            //获得下级部门列表
            string archid = "73dc7faa-0ebc-4ef7-b30b-4691ea4496af";
            List<ArchitectureEntity> entitys = client.GetChildDepartmentEntitys(archid);
            dataGridView1.DataSource = entitys;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            //获得部门下的所有岗位
            string archid = "0454b1b5-17bf-44ed-9066-7dc5056f710c";
            List<ArchitectureEntity> entitys = client.GetDutyEntitys(archid);
            dataGridView1.DataSource = entitys;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            //获得本部门下的所有主管岗位
            string archid = "0454b1b5-17bf-44ed-9066-7dc5056f710c";
            List<ArchitectureEntity> entitys = client.GetLeaderEntitys(archid);
            dataGridView1.DataSource = entitys;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            //获取上级机构Id
            string archid = "0454b1b5-17bf-44ed-9066-7dc5056f710c";
           string id= client.GetFatherId(archid);
           MessageBox.Show(id);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            //把一个用户添加到组织机构中
            string archid = "0454b1b5-17bf-44ed-9066-7dc5056f710c";
            string userid = "50009";
            client.MoveUserIntoArchitecture(userid,archid);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            //删除用户隶属的所有组织关系
            string userid = "50009";
            client.RemoveAllArchitectures(userid);
        }

        private void button11_Click(object sender, EventArgs e)
        {
            //移除组织机构下的一个用户
            string archid = "0454b1b5-17bf-44ed-9066-7dc5056f710c";
            string userid = "50009";
            client.RemoveUser(userid,archid);
        }

        private void button12_Click(object sender, EventArgs e)
        {
            //移除组织机构下的所有用户
            string archid = "0454b1b5-17bf-44ed-9066-7dc5056f710c";
            client.RemoveAllUsers(archid);
        }

        private void button13_Click(object sender, EventArgs e)
        {
            //获得用户上级部门
            string userId = "50009";
            List<ArchitectureEntity> entitys = client.GetHighDepartmentEntitysOfUser(userId);
            dataGridView1.DataSource = entitys;
        }

        private void button14_Click(object sender, EventArgs e)
        {
            //获得用户下级部门
            string userId = "50004";
            List<ArchitectureEntity> entitys = client.GetLowerDepartmentEntitysOfUser(userId);
            dataGridView1.DataSource = entitys;
        }

        private void button15_Click(object sender, EventArgs e)
        {
            //添加一个组织机构
            string archid = "0454b1b5-17bf-44ed-9066-7dc5056f710c";
            ArchitectureEntity entity = new ArchitectureEntity();
            entity.ArchitectureId = Guid.NewGuid().ToString();
            entity.FatherId = archid;//父级部门Id
            entity.Caption = "新建部门";
            entity.DutyId = "4ff4d85c-bbef-46ab-b360-32b4a0e221a6";//所属岗位级别id
            entity.ArchLevel = "总经理";//岗位级别;
            entity.Leader = 1; //是否主管岗位;
            entity.TypeName = WorkFlowConst.Architecture_Department;
            entity.Description = "备注";
            client.Insert(entity);
        }

        private void button16_Click(object sender, EventArgs e)
        {
           // 修改一个组织机构
             string archid = "0454b1b5-17bf-44ed-9066-7dc5056f710c";
            ArchitectureEntity entity = client.GetArchitectureEntity(archid);
            entity.Caption = "修改岗位";
            client.Update(entity);
        }

        private void button17_Click(object sender, EventArgs e)
        {
            //删除组织机构，该方法会强行删除一个组织机构，慎用!删除前检查是否已经包含子机构，如果有子机构不允许删除。
            string archid = "0454b1b5-17bf-44ed-9066-7dc5056f710c";
            client.Delete(archid);
        }

        private void button18_Click(object sender, EventArgs e)
        {
            //获取可以启动的流程
            string userId = "50009";
            List<WorkTaskViewEntity> entitys=  wfClient.GetAllowsStartWorkFlowEntitys(userId);
            dataGridView1.DataSource=entitys;
        }

        private void button21_Click(object sender, EventArgs e)
        {
            //未认领任务
            string userId = "50009"; int pageIndex = 1; int pageSize = 10;
            List<WorkTaskInstanceViewEntity> entitys = wfClient.GetClaimWorkTaskEntity(userId,pageIndex,pageSize);
            dataGridView1.DataSource = entitys;
        }

        private void button20_Click(object sender, EventArgs e)
        {
            //已认领任务
            string userId = "50009"; int pageIndex = 1; int pageSize = 10;
            List<WorkTaskInstanceViewEntity> entitys = wfClient.GetClaimedWorkTaskEntity(userId, pageIndex, pageSize);
            dataGridView1.DataSource = entitys;
        }

        private void button22_Click(object sender, EventArgs e)
        {
            //我参与的任务
            string userId = "50009"; int pageIndex = 1; int pageSize = 10;
            List<WorkTaskInstanceViewEntity> entitys = wfClient.GetAllWorkTaskEntity(userId, pageIndex, pageSize);
            dataGridView1.DataSource = entitys;
        }

        private void button24_Click(object sender, EventArgs e)
        {
            //我已完成的任务
            string userId = "50009"; int pageIndex = 1; int pageSize = 10;
            List<WorkTaskInstanceViewEntity> entitys = wfClient.GetCompletedWorkTaskEntity(userId, pageIndex, pageSize);
            dataGridView1.DataSource = entitys;
        }

        private void button25_Click(object sender, EventArgs e)
        {
            //异常终止的任务
            string userId = "50009"; int pageIndex = 1; int pageSize = 10;
            List<WorkTaskInstanceViewEntity> entitys = wfClient.GetAbnormalWorkTaskEntity(userId, pageIndex, pageSize);
            dataGridView1.DataSource = entitys;
        }

        private void button27_Click(object sender, EventArgs e)
        {
            //正在运行的流程实例
             int pageIndex = 1; int pageSize = 10;
            List<WorkFlowInstanceEntity> entitys = wfClient.GetRunningInstanceEntity(pageIndex, pageSize);
            dataGridView1.DataSource = entitys;
        }

        private void button26_Click(object sender, EventArgs e)
        {
            //挂起的任务
            string userId = "50009"; int pageIndex = 1; int pageSize = 10;
            List<WorkTaskInstanceViewEntity> entitys = wfClient.GetSuspendWorkTaskEntity(userId, pageIndex, pageSize);
            dataGridView1.DataSource = entitys;
        }

        private void tabPage2_Click(object sender, EventArgs e)
        {

        }

        private void button28_Click(object sender, EventArgs e)
        {
            //异常终止的流程实例
            int pageIndex = 1; int pageSize = 10;
            List<WorkFlowInstanceEntity> entitys = wfClient.GetAbnormalInstanceEntity(pageIndex, pageSize);
            dataGridView1.DataSource = entitys;
        }

        private void button29_Click(object sender, EventArgs e)
        {
            //挂起的流程实例
            int pageIndex = 1; int pageSize = 10;
            List<WorkFlowInstanceEntity> entitys = wfClient.GetSuspendInstanceEntity(pageIndex, pageSize);
            dataGridView1.DataSource = entitys;
        }

        private void button30_Click(object sender, EventArgs e)
        {
            //已完成的流程实例
            int pageIndex = 1; int pageSize = 10;
            List<WorkFlowInstanceEntity> entitys = wfClient.GetCompletedInstanceEntity(pageIndex, pageSize);
            dataGridView1.DataSource = entitys;
        }

        private void button19_Click(object sender, EventArgs e)
        {
            //待办任务
            string userId = "50009"; int pageIndex = 1; int pageSize = 10;
            List<WorkTaskInstanceViewEntity> entitys = wfClient.GetTodoWorkTaskEntity(userId, pageIndex, pageSize);
            dataGridView1.DataSource = entitys;
        }

        private void button23_Click(object sender, EventArgs e)
        {
            //启动流程
            string userId = "50009";
            List<WorkTaskViewEntity> entitys = wfClient.GetAllowsStartWorkFlowEntitys(userId);//获取可启动的流程
            if (entitys == null || entitys.Count <= 0) return;
            WorkTaskViewEntity entity = entitys[0];
            string workFlowNo = wfClient.GetWorkFlowNO();//分配一个流程编号
            string Caption = "测试其他流程";
            bool isDraft = false;//是否草稿,true为草稿不提交流程
            //string msg = wfClient.Start(userId, entity.WorkFlowId, entity.WorkTaskId, WorkFlowConst.DefaultCommand, workFlowNo, Caption,isDraft);
            //MessageBox.Show("启动流程执行结果" + msg);

        }

        private void button31_Click(object sender, EventArgs e)
        {
            //提交流程
            string userId = "50009";
            List<WorkTaskInstanceViewEntity> entitys = wfClient.GetClaimedWorkTaskEntity(userId,1,10);//已认领的任务
            if (entitys == null || entitys.Count <= 0) return;
            WorkTaskInstanceViewEntity entity = entitys[0];
            string msg = wfClient.Run(userId,entity.OperatorInstanceId,WorkFlowConst.DefaultCommand);
            MessageBox.Show("提交流程执行结果" + msg);
        }

        private void button32_Click(object sender, EventArgs e)
        {
            List<LogEntity> entitys = wfClient.GetLogEntitys(dtpStartTime.Value, dtpEndTime.Value);
            dataGridView1.DataSource = entitys;
        }

        private void button33_Click(object sender, EventArgs e)
        {
            //增加一个用户
            UserEntity entity = new UserEntity();
            entity.UserId ="50011";
            entity.UserName = "新用户";
            entity.UserDes = "";
            entity.UserSex = "男";
            entity.UserDepart = "销售部";
            entity.UserEmail = "legweifang@sohu.com";
            entity.UserPassword = SysBlack.MD5Encrypt("123");//md5加密
            entity.UserTelephone = "电话号码";
            entity.UserFax = "传真";
            entity.UserMsgAcept = true;//是否接受消息
            entity.UserChgPW = true;//是否允许修改密码
            entity.UserStop = false;//用户是否停用
            entity.UserQQ = "14906007";//QQ号码
            entity.UserMsn = "legweifang@hotmail.com";//msn号码
            
            //判断账号是否存在
            bool b = userClient.Exists(entity.UserId);
            if (b == false)//账号不存在
            {
                userClient.Insert(entity);
            }
        }

        private void button34_Click(object sender, EventArgs e)
        {
            //修改一个用户
            string userId = "50011";
            UserEntity entity = userClient.GetUserEntity(userId);
            entity.UserName = "新用户";
            entity.UserDes = "";
            entity.UserSex = "男";
            entity.UserDepart = "销售部";
            entity.UserEmail = "legweifang@sohu.com";
            entity.UserPassword = SysBlack.MD5Encrypt("123");//md5加密
            entity.UserTelephone = "电话号码";
            entity.UserFax = "传真";
            entity.UserMsgAcept = true;//是否接受消息
            entity.UserChgPW = true;//是否允许修改密码
            entity.UserStop = false;//用户是否停用
            entity.UserQQ = "14906007";//QQ号码
            entity.UserMsn = "legweifang@hotmail.com";//msn号码
            userClient.Update(entity);
        }

        private void button35_Click(object sender, EventArgs e)
        {
            //删除一个用户
            string userId = "50011";
            userClient.Delete(userId);

        }

        private void button36_Click(object sender, EventArgs e)
        {
            //判断是否存在
            string userId = "50011";
            bool b = userClient.Exists(userId);
        }

        private void button37_Click(object sender, EventArgs e)
        {
            //获取一个用户信息
            string userId = "50011";
            UserEntity entity = userClient.GetUserEntity(userId);

        }

        private void button38_Click(object sender, EventArgs e)
        {
            //获取所有用户
            List<UserEntity> entitys = userClient.GetAllUserEntitys();
            dataGridView1.DataSource = entitys;
        }

        private void button39_Click(object sender, EventArgs e)
        {
            //获取用户名
            string userId = "50011";
            string userName = userClient.GetUserNameById(userId);
        }

        private void button40_Click(object sender, EventArgs e)
        {
            //用户验证
            bool b = userClient.LoginCheck("50011", "123");
            if (b)
            {
                MessageBox.Show("登录成功!");
            }
            else
            {
                MessageBox.Show("用户名或者密码错误!");
            }
        }

        private void button41_Click(object sender, EventArgs e)
        {
            //修改密码
            userClient.SetPassword("50011","345");
        }

        private void button42_Click(object sender, EventArgs e)
        {
            //获取用户列表（模糊）byId
            string userId = "50";
            List<UserEntity> entitys = userClient.GetUserEntiysLikeId(userId);
            dataGridView1.DataSource = entitys;
        }

        private void button43_Click(object sender, EventArgs e)
        {
            //获取用户列表（模糊）byName
            string userName = "刘";
            List<UserEntity> entitys = userClient.GetUserEntiysLikeName(userName);
            dataGridView1.DataSource = entitys;
        }

        private void test_Click(object sender, EventArgs e)
        {
            //string msg1 = WorkFlowClient.GetInstance().TaskClaim("lanjialong", "3ae1648c-3974-4941-9cf5-55ef8c6ac1fe");
            //bool ss = WorkFlowClient.GetInstance().IsLock("32624bd5-ba12-4165-9489-797852957a4a");
            string msg = wfClient.Run("xiaxinhua", "00cd7bce-5b10-420d-bce8-c36c1669842b", "提交"); 

            //string msg = wfClient.Start("zhujiyong", "aadf933d-7dc6-4cf8-80d0-b73edf04ac66", "c65b59df-bcf5-4900-96ec-11d5d3bdb31f", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),"20120707889",WorkFlowConst.DefaultCommand,"3","测试表单", false);
            //AuditMessage am = new AuditMessage();
            //am.AuditId = "5fffef4a-f3ed-4adb-9aa9-4a240b99154e";
            //am.WorkflowId = "5fffef4a-f3ed-4adb-9aa9-4a240b99152e";
            //am.WorkflowInsId = "6fffef4a-f3ed-4adb-9aa9-4a240b99154e";
            //am.WorktaskId = "5fff4f4a-f3ed-4adb-9aa9-4a240b99154e";
            //am.WorktaskInsId = "5fffef4a-f3ed-4adb-9aa9-4a290b99154e";
            //am.OperatorInsId = "5fffef4a-f3ed-4adb-98a9-4a240b99154e";
            //am.AuditUserId = "admin";
            //am.AuditUserName = "测试管理员";
            //am.AuditArch = "";
            //am.AuditDuty ="";
            //WorkFlowClient.GetInstance().InsertAuditMessage(am);
            //WorkFlowEntity entity=WorkFlowClient.GetInstance().GetWorkFlowEntity("7b23d86d-006a-40b8-9f90-51105d1d4ed3");  //获取实体流程
            //DataTable dt = WorkFlowClient.GetInstance().GetWorkTaskFormsView("7fb02e7f-7a6c-4a4e-833f-573cb5f8ebe9");   //获得任务节点与表单的视图信息
            //string msg1 = WorkFlowClient.GetInstance().TaskClaim("zhouyaobing", "d27dd6bf-910c-4f65-b21c-8c6096ce25f8");
            //string msg = WorkFlowClient.GetInstance().WorkTaskAssign("lanjialong", "dongshiqiao", "90bf4d7e-21a7-49a7-a444-238511622e63");
           // DataTable dt = WorkFlowClient.GetInstance().GetAccreditList("and WFClassId = '4e4fc5ad-2b80-40e0-ba84-715c03891bc3'");


            //List<AccreditUserEntity> AccreditUserList = new List<AccreditUserEntity>();//代理实体集合

            //string AccreditToName = "陈伟";//被代理人名称
            //string AccreditToUserId = "chenwei";//被代理人ID
            //string AccreditFromName = "汪定新";//代理人名称
            //string AccreditFromUserId = "wangdingxin";//代理人ID
            //DateTime AccreditDateTime = DateTime.Now;//创建时间
            //char AccreditStatus = '1';//代理状态
            //DateTime AcceditStartTime = DateTime.Now;//开始时间
            //DateTime AcceditEndTime = DateTime.Now.AddMonths(1);//结束时间
            //string AccreditName = "陈伟";//创建人
            //char AccreditEstablish = '0'; //代理流程创建
            //char AccreditMatters = '0';//待办事宜
            //string AccreditfStatus = "1";//
            //string txtvlues = "0ff69eb0-eab9-4499-9c56-d54a2ce872bd,3444ab76-bea9-4b94-827f-c0573f16db6a,ac3f331d-8bf5-49f0-9f77-0811b0c1a2eb,6a61e02f-9487-44d5-885c-6f56556f5810";
            //if (txtvlues.ToString() != "")
            //{
            //    string[] str = txtvlues.Split(',');//流程ID
            //    if (str.Length > 0)
            //    {
            //        for (int i = 0; i < str.Length; i++)
            //        {
            //            AccreditUserEntity AccreditUser = new AccreditUserEntity(); //代理实体
            //            AccreditUser.AccreditId = Guid.NewGuid().ToString();
            //            AccreditUser.AccreditToName = AccreditToName;
            //            AccreditUser.AccreditToUserId = AccreditToUserId;
            //            AccreditUser.AccreditFromName = AccreditFromName;
            //            AccreditUser.AccreditFromUserId = AccreditFromUserId;
            //            AccreditUser.AccreditDateTime = AccreditDateTime;
            //            AccreditUser.AccreditStatus = AccreditStatus;
            //            AccreditUser.AcceditStartTime = AcceditStartTime;
            //            AccreditUser.AcceditEndTime = AcceditEndTime;
            //            AccreditUser.AccreditName = AccreditName;
            //            AccreditUser.AccreditEstablish = AccreditEstablish;
            //            AccreditUser.AccreditMatters = AccreditMatters;
            //            AccreditUser.AccreditfStatus = AccreditfStatus;
            //            AccreditUser.WorkFlowId = str[i];
            //            AccreditUserList.Add(AccreditUser);
            //        }
            //        bool reust = WorkFlowClient.GetInstance().AddAccreditUser(AccreditUserList);
            //    }
            //}

            //DataTable dt = WorkFlowClient.GetInstance().GetDeptAgency("wangtao");


            //AffixFiles entity = new AffixFiles();
            //    entity.AffixId = Guid.NewGuid().ToString();
            //    entity.WorkflowId = Guid.NewGuid().ToString();
            //    entity.WorkflowInsId = Guid.NewGuid().ToString();
            //    entity.WorktaskId = Guid.NewGuid().ToString();
            //    entity.WorktaskInsId = Guid.NewGuid().ToString();
            //    entity.FileNameId = Guid.NewGuid().ToString() + ".dll";
            //    entity.FileName = "Test";
            //    entity.FilePath = "D:\\HF2.0\\source\\Web\\WebApplication\\Files\\0cd26032-eeeb-45cb-addf-ae0c8cc5a553.dll";
            //WorkFlowClient.GetInstance().InsertAffixcrm(entity);

        }

      
    }
}
